package com.jiudaoliu.homeprototype.handle;

import com.jiudaoliu.homeprototype.POJO.VO.CommonVO;
import com.jiudaoliu.homeprototype.exception.CommonException;
import com.jiudaoliu.homeprototype.utils.CommonVO_Utils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Package com.styles.codingstyle.Handle
 * @Description: 统一异常处理
 * @Author 陈建
 * @Create: 2018-05-29 11:55
 * @Version 1.0
 * @Update: message
 */
@ControllerAdvice
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonVO exceptionHandle(Exception e) {
        if (e instanceof CommonException) {
            return CommonVO_Utils.fail(((CommonException) e).getCode(), e.getMessage());
        }



        CommonVO commonVO = CommonVO_Utils.fail_500(e.getMessage());
        return commonVO;
    }

   /* @ExceptionHandler(value = NoViewException.class)
    public ModelAndView noViewExceptionHandle(Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        if (e instanceof NoViewException) {
            modelAndView.setViewName("/index");
        }
        modelAndView.setViewName("/index");
        return modelAndView;
    }*/
}
